import { _decorator, Component, Node } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { UIExitAnimtions } from '../../Init/Tools/UIExitAnimations';
import { EventTypes } from '../../Init/Managers/EventTypes';
import EventManager from '../../Init/Managers/EventManager';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
const { ccclass, property } = _decorator;

@ccclass('RecoverUI')
export class RecoverUI extends BasicUI {

    @property(Node)
    bg: Node = null;
    @property(Node)
    panel: Node = null;

    recoverHP = 1;


    public show(d?: any): void {
        super.show();
        // 视频按钮界面展示LOG
        EventManager.emit(EventTypes.SDKEvents.ShowAdButton, GlobalEnum.VideoIdx.RecoverHP);
        UISystem.showUI(UIEnum.PlayerAssetsUI);
    }

    public hide(d?: any): void {
        super.hide();
        UISystem.hideUI(UIEnum.PlayerAssetsUI);
    }

    showCloseAnim(cb) {
        let anim1 = this.bg.getComponent(UIExitAnimtions);
        let anim2 = this.panel.getComponent(UIExitAnimtions);
        anim1.showHideAnim(false);
        anim2.showHideAnim(false, () => {
            cb && cb();
        }, true)
    }

    onClose() {
        this.showCloseAnim(() => {
            UISystem.hideUI(UIEnum.RecoverUI);
            // 取消复活->失败
            EventManager.emit(EventTypes.GameEvents.GameOver, false, 0.2);
        })
    }
    // 视频获取
    onVideoClick() {
        EventManager.emit(EventTypes.SDKEvents.ShowVideo, () => {
            // 恢复生命值 -todo
                    
            // 关闭UI
            this.showCloseAnim(() => {
                UISystem.hideUI(UIEnum.RecoverUI);
            })
        }, GlobalEnum.VideoIdx.RecoverHP);
    }
    _price = 200;
    // 金币获取
    onGoldClick() {
        // 判断金币数量
        const data = StorageSystem.getData().userAssets;
        if (data.asset >= this._price) {
            data.asset -= this._price;
            StorageSystem.saveData();
            StorageSystem.updateToAssets(false);
            // 恢复生命值 -todo
            
            // 关闭UI
            this.showCloseAnim(() => {
                UISystem.hideUI(UIEnum.RecoverUI);
            })
        } else {
            EventManager.emit(EventTypes.GameEvents.ShowTips, '金币不足！');
        }
    }
}

